import PagePromoterUtil, { PagePromoter } from '../../../data/common/pagePromoter';
import AppSceneUtil, {PageScene} from '../../../data/common/appScene';
import SessionUtil from "../../../data/session/session"
import SystemUtil from '../../../shell/system/system';
const Common = require('../../../utils/common.js');

Page({
    data: {
        token: '',
        itemId: 43,
        itemInfo: {},
        indicatorDots: true,
        duration: 500,
        detail: {},
        imageSize: [],
    },
    onLoad: function (options) {
        // 开启转发按钮
        PagePromoterUtil.showShareMenu(true).then()
        SessionUtil.getInstance().then(session=>{
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this)
            if (options.itemId) {
                this.setData({
                    itemId: options.itemId
                })
            }
            wx.setNavigationBarTitle({
                title: '商品详情'
            })
            wx.showLoading({
                title: '数据加载中',
            })
            let pageScene = new PageScene(this)
            if (!pageScene.hasPromoter()) {
                this.itemInfo();
            }
            /**获取屏幕宽度 */
            this.setData({
                w_width: SystemUtil.getSystemInfo(false).windowWidth,
            })
        })
    },
    onShow: function(){
        SessionUtil.getInstance().then(()=>{
            // 注册当前页面的场景信息
            AppSceneUtil.setPageScene(this, false)
        })
    },
    itemInfo: function (action = '') {
        // 加载详情
        var page = this;
        var itemId = page.data.itemId;
        if (action == 'Refresh') {
            wx.stopPullDownRefresh();
        }
        Common.my_request({
            url: '/AppMaster/Product/productDetail',
            data: {
                productId: itemId
            },
            success: function (res) {
                if (res.data.result == 0) {
                    var Product = res.data.data;
                    if (!Product) {
                        Common.Alert('错误提示', res.data.message);
                        wx.hideLoading();
                        var pages = getCurrentPages();
                        if (pages.length > 2) {
                            wx.navigateBack()
                        } else {
                            wx.switchTab({
                                url: '/pages/index/index',
                            })
                        }
                        return;
                    }
                    var str = Product.detail;
                    var reg = /(http.*?(jpg|jpeg|gif|png))/ig;
                    var result = str.match(reg);
                    
                    page.setData({
                        itemInfo: Product,
                        detail: result,
                    });
                    // 关闭加载
                    wx.hideLoading();
                } else {
                    console.log(res.data.message);
                    Common.Alert('错误提示', res.data.message);
                    wx.hideLoading();
                    var pages = getCurrentPages();
                    if (pages.length > 2) {
                        wx.navigateBack()
                    } else {
                        wx.switchTab({
                            url: '/pages/index/index',
                        })
                    }
                    return;
                }
            }
        });
    },
    onPullDownRefresh: function () {
        // 下拉刷新
        var page = this;
        wx.showLoading({
            title: '数据加载中',
        })
        page.itemInfo('Refresh');
    },
    imageLoad: function (e) {
        var index = e.currentTarget.id;
        var page = this;
        var $width = e.detail.width,    //获取图片真实宽度
            $height = e.detail.height,
            ratio = $width / $height;    //图片的真实宽高比例
        var viewWidth = page.data.w_width - 20,           //设置图片显示宽度，左右留有20rpx边距
            viewHeight = viewWidth / ratio;    //计算的高度值
        var imageHeight = page.data.imageSize;

        imageHeight[index] = viewHeight;

        page.setData({
            imageSize: imageHeight
        })
    },
    onShareAppMessage: function () {
        // 构造并返回自定义数据
        return (new PagePromoter( this.route, { itemId : this.data.itemId }))
        .setShareTitle(this.data.itemInfo.productName)
        .buildShareInfo()
    },
   
    clearStorageSync: function () {
        wx.setStorageSync("dilivery", '');
        wx.setStorageSync("invoice", {});
        wx.setStorageSync("pay", {});
    },
   
    toIndex(){
        wx.switchTab({
            url: '/pages/index/index',
        })
    }
})